package com.gxa.controller;


import com.gxa.exception.CustomerException;
import com.gxa.pojo.Manager;
import com.gxa.service.ManagerService;
import com.gxa.util.JsonResult;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @author 伍炳清
 * @date 2020-09-21 18:56
 */
@Controller
@RequestMapping("/manager")

public class ManagerController {

    @Resource
    private ManagerService managerService;
    JsonResult jsonResult = new JsonResult();

    //登录
    @PostMapping(value = "/login")
    @ResponseBody
    public Map login(@RequestBody Map<String, String> map){

        try {
            Manager manager = managerService.login(map.get("username"), map.get("password"));
            jsonResult.setCode("200");
            jsonResult.setMsg("登录成功");
            jsonResult.setData(manager);

        } catch (CustomerException e) {
            jsonResult.setCode("500");
            jsonResult.setMsg("登录失败:" + e.getMessage());
        }

        return jsonResult.getValues();
    }

    //添加管理员
    @RequestMapping("/add")
    @ResponseBody
    public Map add(@RequestBody Map<String, String> map) {

        Manager manager = new Manager();

        manager.setName(map.get("name"));
        manager.setSex(Integer.valueOf(map.get("sex")));
        manager.setUsername(map.get("username"));
        manager.setPassword(map.get("password"));
        manager.setPhone(map.get("phone"));

        try {
            managerService.addManager(manager);
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
            jsonResult.setData(manager);
        } catch (CustomerException e) {
            jsonResult.setCode("500");
            jsonResult.setMsg("添加失败:" + e.getMessage());
            e.printStackTrace();
        }
        return jsonResult.getValues();
    }

    //管理员修改密码
    @RequestMapping("/updatePassword")
    @ResponseBody
    public Map updatePassword(@RequestBody Map<String, String> map) {

        try {
            managerService.updatePassword(map.get("username"),
                    map.get("oldPassword"),
                    map.get("newPassword"));
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        } catch (CustomerException e) {
            jsonResult.setCode("500");
            jsonResult.setMsg("修改失败:" + e.getMessage());
        }
        return jsonResult.getValues();
    }

    //管理员修改基本信息
    @RequestMapping("/updateManager")
    @ResponseBody
    public Map updateManager(@RequestBody Map<String, String> map) {

        Manager manager = new Manager();
        manager.setId(Integer.valueOf(map.get("id")));
        manager.setName(map.get("name"));
        manager.setSex(Integer.valueOf(map.get("sex")));
        manager.setUsername(map.get("username"));
        manager.setPhone(map.get("phone"));

        try {
            managerService.updateManager(manager);
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        } catch (CustomerException e) {
            jsonResult.setCode("500");
            jsonResult.setMsg("修改失败:" + e.getMessage());
        }

        return jsonResult.getValues();
    }

    //通过id查询管理员
    @RequestMapping("/findManagerById")
    @ResponseBody
    public Map findManagerById(@RequestBody Map<String, String> map) {

        try {
            Manager manager = managerService.findManagerById(Integer.valueOf(map.get("id")));
            jsonResult.setCode("200");
            jsonResult.setMsg("查找成功");
            jsonResult.setData(manager);
        } catch (CustomerException e) {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败:" + e.getMessage());
        }


        return jsonResult.getValues();

    }

    //通过id删除管理员
    @RequestMapping("/deleteManagerById")
    @ResponseBody
    public Map deleteManagerById(@RequestBody Map<String, String> map) {

        try {

            managerService.deleteManager(Integer.valueOf(map.get("id")));

            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        } catch (CustomerException e) {
            jsonResult.setCode("500");
            jsonResult.setMsg("删除失败:" + e.getMessage());
        }
        return jsonResult.getValues();

    }

}
